package com.cssw.fyzb.busi.api.user;

import com.cssw.fyzb.core.model.dodb.User;
import com.cssw.fyzb.core.model.vo.user.InviteCodeVo;
import com.cssw.fyzb.busi.service.user.UserService;
import com.cssw.fyzb.busi.util.CodeUtil;
import com.ydn.appserver.Action;
import com.ydn.appserver.Request;
import com.ydn.appserver.Response;
import com.ydn.appserver.annotations.Function;
import com.ydn.appserver.annotations.Parameter;
import com.ydn.appserver.annotations.Type;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * @author Feng Chen
 */
@Function(description = "获取邀请码", parameters = {
        @Parameter(name = "userId", type = Type.String, description = "用户编号", required = true)
})
@Component
public class GetInvitCode implements Action {

    private final static Logger logger = LoggerFactory.getLogger(GetInvitCode.class);

    @Value("${invite.url}")
    private String inviteUrl;

    @Autowired
    private UserService userService;

    @Override
    public Response execute(Request request) throws Exception {
        try {
            Long userId = request.getLong("userId");
//            User user = userService.getById(userId);
//            return Response.success().put("code", user.getMobile());
//            return Response.success().put("code", CodeUtil.encode(userId));
            User user = userService.getById(userId);
            InviteCodeVo inviteCodeVo = new InviteCodeVo();
            //判断是否实名
            if (user.getFaceAuth().equals("1")) {
                inviteCodeVo.setCode(CodeUtil.encode(userId));
                inviteCodeVo.setCodeType("BT016");
                inviteCodeVo.setUrl(inviteUrl + CodeUtil.encode(userId));
                inviteCodeVo.setFaceAuth("1");
            } else {
                inviteCodeVo.setFaceAuth("0");
            }
            return Response.success().put("data", inviteCodeVo);
        } catch (Exception e) {
            logger.error("", e);
            return Response.fail("获取邀请码失败");
        }
    }
}
